<?php
	
	class EventItemBusiness {
		
		public function selectEventItemByEventId($eventId) {
			
			$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
			
			$dataBaseConnection->connect();
			
			$eventItemDataAccess = DataAccessFactory::factory('EventItemDataAccessMySQL');
			
			$result = $eventItemDataAccess->selectEventItemByEventId($eventId, $dataBaseConnection);
			
			$dataBaseConnection->disconnect();
			
			return $result;
		}
		
		public function select($eventItem) {
			
			$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
			
			$dataBaseConnection->connect();
			
			$eventItemDataAccess = DataAccessFactory::factory('EventItemDataAccessMySQL');
			
			$result = $eventItemDataAccess->select($eventItem, $dataBaseConnection);
			
			$dataBaseConnection->disconnect();
			
			return $result;
		}

		public function saveEventItemAttendant($eventItem, $dataBaseConnection = null) {
			
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$eventItemDataAccess = DataAccessFactory::factory('EventItemDataAccessMySQL');
			
			$result = $eventItemDataAccess->saveEventItemAttendant($eventItem, $dataBaseConnection);
			
			if ($connectionOpened) {
				if (!$result) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result;
		}
		
		// public function deleteEventItemAttendant($eventItem, $dataBaseConnection = null) {
// 			
			// $connectionOpened = false;
			// if ($dataBaseConnection == null) {
				// $connectionOpened = true;
				// $dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				// $dataBaseConnection->connect();
				// $dataBaseConnection->beginTransaction();
			// }
// 			
			// //TODO DELETAR COMENTARIOS DO USUARIO
			// $eventItemDataAccess = DataAccessFactory::factory('EventItemDataAccessMySQL');
// 			
			// $result = $eventItemDataAccess->deleteEventItemAttendant($eventItem, $dataBaseConnection);
// 			
			// if ($connectionOpened) {
				// if (!$result) {
					// $dataBaseConnection->rollback();
				// } else {
					// $dataBaseConnection->commit();
				// }
				// $dataBaseConnection->disconnect();
			// }
// 			
			// return $result;
		// }
		 
		 //TODO passar pra EventItemUserBusiness  pois estah chamando uma funcao q nao estah mais aqui
		public function deleteEventItemAttendantByRegistrationCode($registrationCode, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$eventItem = new EventItem();
			$eventItem->getEventItemUser()->getEventUser()->setUserRegistrationCode($registrationCode);
			
			$result = $this->deleteEventItemAttendant($eventItem, $dataBaseConnection);
			
			if ($connectionOpened) {
				if (!$result) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}			
			return $result;
		}
	}

?>